<?php

namespace Admin\Controller;

use Think\Controller;
use JohnLui\AliyunOSS;
use lyf\Str;

require './vendor/autoload.php';

class BannerController extends BaseController
{

    /**
     * @desc banner 列表
     * @author Egor<http://gitee.com/seosite>
     */
    public function lists()
    {
        $News = M('banner');
        $count = $News->count();
        $page = new \Think\Page($count, 10);
        $show = $page->show();
        $list = $News->limit($page->firstRow . ',' . $page->listRows)->order('id DESC')->select();

        foreach ($list as $key => $val) {
            $list[$key]['goods_name'] = M('vote')->where(['id'=>$val['band_id']])->getField('title');
            $list[$key]['create_time'] = date('Y-m-d H:i:s', $val['create_time']);
        }

        $this->assign('show', $show);
        $this->assign('list', $list);
        $this->display();
    }

    /**
     * @desc 删除
     * @author Egor<http://gitee.com/seosite>
     */
    public function del()
    {
        $id = I('id');
        M('Banner')->where("id = $id")->delete();
        $this->success("删除成功");
    }


    /**
     * @desc 添加
     * @author Egor<http://gitee.com/seosite>
     */
    public function add()
    {
        if ($_POST) {

            $data = $_POST;

            if (!$data['band_id']) {
                $this->error('绑定商品不能为空,添加失败！');
            }

            if (!$data['files1']) {
                $this->error('广告图不能为空,添加失败！');
            }

            $insert_data = [
                'cover_url'     =>  $data['files1'],
                'band_id'       =>  $data['band_id'],
                'band_type'     =>  '',
                'num'           =>  empty($data['numbers'])?50:$data['numbers'],
                'create_time'   => time()
            ];
            $operation = M('banner')->add($insert_data);

            if ($operation) {
                $this->success('添加成功！', U('Banner/add'));
            } else {
                $this->error('添加失败！');
            }
        } else {

            $map['status'] = 1;
            $goods_list = M('vote')->field('id,title')->where($map)->select();

            $this->assign('goods_list',$goods_list);
            $this->display();
        }

    }

    //广告修改
    public function edit()
    {

        if ($_POST) {
            $data = $_POST;
            $data['ceate_ip'] = client_ip();
            $data['status'] = 1;
            $data['headsmall'] = $data['files'];
            $where['id'] = $data['id'];
            //var_dump($data);
            $baikeres = M('Banner')->where($where)->save($data);
            if ($baikeres) {
                $this->success('修改成功！', U('Banner/lists'));
            } else {
                $this->error('修改失败!');
            }
        } else {
            $id = I('id');
            $news = M('Banner')->where("id = $id")->find();
            if ($news) {
                $this->assign('news', $news);
                $this->display();
            } else {
                $this->error('没有该产品!');
            }
        }
    }
}


?>